/**
 * Created by wry on 2015/2/12.
 */
'use strict';

angular.module('backofficeApp.retailerCommunity.controllers', []).
    controller('retailerCommunityCtrl', ['$scope', '$http', '$location', 'CommunityService', function ($scope, $http, $location, communityService) {
        $scope.communityLabelList = communityService.getAllCommunityService();

        $scope.gotoCooperation = function (communityId) {
            $location.path('/retailerCommunity/' + communityId);
        }
    }]).
    controller('cooperationCtrl', ['$scope', '$http', '$location', '$routeParams', function ($scope, $http, $location, $routeParams) {
        var communityId = $routeParams.communityId;


        $scope.currentPage = 1;
        $scope.totalPage = 0;
        $scope.pages = [];


        $scope.getBranchRetailer = function () {
            $http.get('http://' + backOfficeHost + '/rest/v1.0/backoffice/voucher/getBranchRetailer/' + communityId + '/' + $scope.currentPage)
                .success(function (data) {
                    $scope.totalPage = data.data.totalPage;
                    $scope.retailerList = data.data.list;
                })
        };
        $scope.getBranchRetailer();

        $scope.bindRetailer = function (flag, chk, retailerId) {
            if (chk == null && flag == null) {
                chk = false;
            } else if (chk == null && flag != null) {
                chk = true;
            }
            var input = {
                id: retailerId,
                communityId: communityId,
                status: chk
            };

            $http.post('http://' + backOfficeHost + '/rest/v1.0/backoffice/voucher/singleBindRetailer', input, {
                headers: {'Content-Type': 'application/json;charset=utf-8'}
            }).success(function (data) {
            }).error(function () {
            });
        };

        $scope.searchRetailer = function (opt) {
            opt.parentId = -1;
            opt.communityId = communityId;

            $http.post('http://' + backOfficeHost + '/rest/v1.0/backoffice/voucher/searchRetailerByName', opt, {
                headers: {'Content-Type': 'application/json;charset=utf-8'}
            }).success(function (data) {
                $scope.retailerList = data.data;
            }).error(function () {
            });
        }

        $scope.onSelectPage = function (page) {
            $scope.currentPage = page;
            $scope.getBranchRetailer();
        }

    }]).directive('mutau', function () {
        return {
            restrict: 'E',
            template: '',
            replace: true,
            link: function (scope, element, attrs) {
                scope.$watch('currentPage', function (value) {
                    scope.pages = [];
                    for (var i = value - 5 > 0 ? value - 5 : 1; i <= (value + 5 > scope.totalPage ? scope.totalPage : value + 5); i++) {
                        scope.pages.push(i);
                    }
                });
                scope.$watch('totalPage', function (value) {
                    scope.pages = [];
                    for (var i = scope.currentPage - 5 > 0 ? scope.currentPage - 5 : 1; i <= (scope.currentPage + 5 > value ? value : scope.currentPage + 5); i++) {
                        scope.pages.push(i);
                    }
                });

                scope.isActive = function (page) {
                    return scope.currentPage === page;
                };
                scope.selectPage = function (page) {
                    if (!scope.isActive(page)) {
                        scope.currentPage = page;
                        scope.onSelectPage(page);
                    }
                };
                scope.selectPrevious = function () {
                    if (!scope.noPrevious()) {
                        scope.selectPage(scope.currentPage - 1);
                    }
                };
                scope.selectNext = function () {
                    if (!scope.noNext()) {
                        scope.selectPage(scope.currentPage + 1);
                    }
                };
                scope.noPrevious = function () {
                    return scope.currentPage == 1;
                };
                scope.noNext = function () {
                    return scope.currentPage == scope.totalPage;
                };
            }
        };
    });

